package org.example.acm;

import java.util.Scanner;

public class TernaryTreeHeightSolution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = scanner.nextInt();
        int[] nums = new int[count];
        for (int i = 0; i < count; i++) {
            nums[i] = scanner.nextInt();
        }
        TernaryTreeHeightSolution solution = new TernaryTreeHeightSolution();
        TernaryTree ternaryTree = null;
        for (int i=0;i<count;i++) {
            ternaryTree = solution.insert(ternaryTree, nums[i]);
        }
        int height = solution.getTreeHeight(ternaryTree);
        System.out.println(height);
    }

    private TernaryTree insert(TernaryTree tree, int value) {
        if (tree == null) {
            return new TernaryTree(value);
        }
        if (value > tree.value + 500) {
            tree.right = insert(tree.right, value);
        } else if (value < tree.value - 500) {
            tree.left = insert(tree.left, value);
        } else {
            tree.mid = insert(tree.mid, value);
        }
        return tree;
    }

    private int getTreeHeight(TernaryTree tree) {
        if (tree == null) {
            return 0;
        }
        // 递归计算左右子树和中间子树的高度，返回最大值加 1
        return Math.max(Math.max(getTreeHeight(tree.left), getTreeHeight(tree.right)),
                getTreeHeight(tree.mid)) + 1;
    }

    // 三叉树节点定义
    static class TernaryTree {
        private int value;
        private TernaryTree left;
        private TernaryTree mid;
        private TernaryTree right;

        TernaryTree(int val) {
            this.value = val;
        }
    }
}
